﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using AllenalaMente.State;
using System.Collections.ObjectModel;

namespace AllenalaMente.Menu
{
    public partial class GamesListTest : PhoneApplicationPage
    {
        public class SchemasVoice
        {
            public int index { get; set; }
            public string path { get; set; }
            public string name { get; set; }
            public string bestScore { get; set; }
            public string color { get; set; }
        }

        private GameState gameState;

        private bool RefreshScore = false;
        private SchemasVoice selection;

        public GamesListTest()
        {
            InitializeComponent();

            PageTitle.Text = Locale.Strings.GameList;
            TextName.Text = Locale.Strings.Game.ToUpper();
            TextScore.Text = Locale.Strings.BestScore.ToUpper();
            gameState = GameStateManager.GetState();
            textBlockComix.Text = Locale.Strings.PractiseTextBlock;
            buttonGo.Content = Locale.Strings.ButtonStart;

            this.Loaded += GamesListTest_Loaded;

            Animation.FadeIn(this, 500);
        }

        private void UpdateScores()
        {
            // Elenco Giochi
            var Schemas = new ObservableCollection<SchemasVoice>();
            Schemas.Clear();

            for (int i = 0; i < gameState.GamesList.Count; i++)
            {
                int id = i;
                string iconPath = "/AllenalaMente;component/" + gameState.GamesList[i].iconPath;
                string gameName = gameState.GamesList[i].gameId.ToString();
                string gameBestScore = gameState.GamesList[i].recordScore.ToString();

                string col = "gray";

                switch (gameState.GamesList[i].gameCategory)
                {
                    case GameCategories.Memory: col = "green"; break;
                    case GameCategories.Math: col = "yellow"; break;
                    case GameCategories.Logic: col = "cyan"; break;
                    case GameCategories.Reflex: col = "red"; break;
                }

                if (gameName != "random")
                    Schemas.Add(new SchemasVoice() { index = id, path = iconPath, name = gameName.Substring(3), bestScore = gameBestScore, color = col });


            }

            listBoxGames.ItemsSource = Schemas;
        }

        void GamesListTest_Loaded(object sender, RoutedEventArgs e)
        {
            if (!RefreshScore)
            {
                UpdateScores();
                RefreshScore = true;
            } 
        }

        private void buttonGo_Click(object sender, RoutedEventArgs e)
        {
            //SchemasVoice item = listBoxGames.SelectedItem as SchemasVoice;

            if (selection == null) return;

            gameState.gameMode = GameMode.Practice;
            gameState.chosenGames[0] = selection.index;
            gameState.chosenGames[1] = gameState.chosenGames[2] = gameState.chosenGames[3] = -1;

            RefreshScore = false;

            NavigationService.Navigate(new Uri("/AllenalaMente;component/Games/GamePage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void listBoxGames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selection = listBoxGames.SelectedItem as SchemasVoice;
        }
    }
}